#include "Plane.h"
#include "ForceFriction.h"

Plane::Plane(void)
{
	normal = Vector3f(0.0f,1.0f,0.0f); //regular straight floor plane
	origin = Vector3f::zero;
	damping = 0.3f;
	
}

Plane::Plane(Vector3f _normal, Vector3f _origin, float _damping) 
{
	normal = _normal;
	origin = _origin;
	damping = _damping;
	normal.normalize();
}

Plane::Plane(const Plane& other) 
{
	normal = other.normal;
	origin = other.origin;
	damping = other.damping;
	normal.normalize();
}


LimitedPlane::LimitedPlane(void) {
	v1 = Vector3f(10.0f,10.0f,0.0f);
	v1 = Vector3f(100.0f,10.0f,0.0f);
	normal = Vector3f(0.0f,1.0f,0.0f); //regular straight floor plane, dependant on v1 and v2
	damping = 0.3f;
	frictionGenerator = new ForceFriction(this, 0.7f, 0.6f);
}


LimitedPlane::~LimitedPlane(void) {
	
	if(frictionGenerator)
		delete frictionGenerator;
}

LimitedPlane::LimitedPlane(Vector3f _v1, Vector3f _v2, float _damping) {
	
	v1 = _v1;
	v2 = _v2;
	normal = v2-v1;
	CL_Pointf CL_normal(-normal.y, normal.x);
	normal = Vector3f(-normal.y, normal.x, 0.0f);

	float CL_length = CL_normal.length();
	float length = normal.length();

	normal.normalize();
	CL_normal.normalize();
	
	damping = _damping;
	frictionGenerator = new ForceFriction(this, 0.1f, 0.08f);
}

LimitedPlane::LimitedPlane(const LimitedPlane& other) {
	
	v1 = other.v1;
	v2 = other.v2;
	normal = other.normal;
	damping = other.damping;
	frictionGenerator = new ForceFriction(this, 0.1f, 0.08f);
}

float LimitedPlane::isLeft(Vector3f c) {
    return ((v2.x - v1.x)*(c.y - v1.y) - (v2.y - v1.y)*(c.x - v1.x)) > 0; //determinant
}

//http://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment
bool LimitedPlane::pointIsOnPlane(Vector3f p) {
	float dotproduct = (v2-v1).dot(p-v1);
	float squared_distance = (v2.x - v1.x)*(v2.x - v1.x) + (v2.y - v1.y)*(v2.y - v1.y) ;

	return (dotproduct >= 0 && dotproduct < squared_distance);
}